package br.com.mayco.util;

import java.util.Random;

/**
 * @author Mayco Lucian
 * @version 1.0.0
 * @since 25/02/2013
 */
public class PasswordUtil {

    private boolean useLetters;
    private boolean useLettersUpper;
    private boolean useNumbers;
    private boolean useSimbols;
    private int passwordSize;

    public PasswordUtil() {
    }

    public PasswordUtil(boolean useLetters, boolean useLettersUpper, boolean useNumbers, boolean useSimbols, int passwordSize) {
        this.useLetters = useLetters;
        this.useLettersUpper = useLettersUpper;
        this.useNumbers = useNumbers;
        this.useSimbols = useSimbols;
        this.passwordSize = passwordSize;
    }

    public PasswordUtil(boolean useLetters, boolean useNumbers) {
        this.useLetters = useLetters;
        this.useLettersUpper = false;
        this.useNumbers = useNumbers;
        this.useSimbols = false;
        this.passwordSize = 6;
    }

    public String getPassword() {
        String data = getData();
        StringBuilder password = new StringBuilder(passwordSize);
        Random rnd = new Random();
        while (password.length() < passwordSize) {
            int x = rnd.nextInt(data.length());
            char c = data.charAt(x);
            password.append(c);
        }
        return password.toString();
    }

    private String getData() {
        StringBuilder possibilities = new StringBuilder();
        if (useLetters) {
            possibilities.append(PasswordChars.LETTERS_LOWER);
        }
        if (useLettersUpper) {
            possibilities.append(PasswordChars.LETTERS_UPPER);
        }
        if (useNumbers) {
            possibilities.append(PasswordChars.NUMBERS);
        }
        if (useSimbols) {
            possibilities.append(PasswordChars.SIMBOLS);
        }
        String data = possibilities.toString();
        return data;
    }

    public String getLettersLower() {
        return PasswordChars.LETTERS_LOWER;
    }

    public String getLettersUpper() {
        return PasswordChars.LETTERS_UPPER;
    }

    public String getNumbers() {
        return PasswordChars.NUMBERS;
    }

    public String getSimbols() {
        return PasswordChars.SIMBOLS;
    }

    public boolean isUseLetters() {
        return useLetters;
    }

    public void setUseLetters(boolean useLetters) {
        this.useLetters = useLetters;
    }

    public boolean isUseLettersUpper() {
        return useLettersUpper;
    }

    public void setUseLettersUpper(boolean useLettersUpper) {
        this.useLettersUpper = useLettersUpper;
    }

    public boolean isUseNumbers() {
        return useNumbers;
    }

    public void setUseNumbers(boolean useNumbers) {
        this.useNumbers = useNumbers;
    }

    public boolean isUseSimbols() {
        return useSimbols;
    }

    public void setUseSimbols(boolean useSimbols) {
        this.useSimbols = useSimbols;
    }

    public int getPasswordSize() {
        return passwordSize;
    }

    public void setPasswordSize(int passwordSize) {
        this.passwordSize = passwordSize;
    }
}
